package com.grasp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import cc.wulian.ihome.wan.NetSDK;
import cc.wulian.ihome.wan.util.MD5Util;

import com.grasp.base.ActionBar;
import com.grasp.base.BaseActivity;
import com.grasp.base.ConstantBase;
import com.grasp.base.Logger;

public class LoginActivity extends BaseActivity {

	private static LinearLayout loginLayout;
	private EditText phoneNumberEdit;
	private EditText passwordEdit;
	private String mAppID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_login);
		loginLayout = (LinearLayout) findViewById(R.id.login_server_layout);
		phoneNumberEdit = (EditText) findViewById(R.id.phone_number_edit);
		passwordEdit = (EditText) findViewById(R.id.password_edit);
		phoneNumberEdit.setText("00CF8B23250E");
		passwordEdit.setText("198711");
		ActionBar mActionBar = (ActionBar) findViewById(R.id.logo_actionbar);
		mActionBar.setLeftBtnVisible(false);
		mActionBar.setRightBtnVisible(false);
		findViewById(R.id.forget_password_view).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Toast.makeText(LoginActivity.this, "ForgetPassword",
								Toast.LENGTH_SHORT).show();
					}
				});
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		mAppID = tm.getDeviceId();
		System.out.println("===mAppID_1:" + mAppID);
		if (TextUtils.isEmpty(mAppID)) {
			mAppID = Secure.getString(this.getContentResolver(),
					Secure.ANDROID_ID);
		}
		System.out.println("===mAppID_2:" + mAppID);
	}

	public void userLogin(View v) {

		new Thread() {
			public void run() {
				mHandler.sendEmptyMessage(ConstantBase.LOADING);
				try {
					sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("===>isValid:" + NetSDK.isValid());
				System.out.println("===>isConnected:" + NetSDK.isConnected());
				String phoneNumberStr = phoneNumberEdit.getText().toString();
				String passwordStr = passwordEdit.getText().toString();
				System.out.println("===>userinfo:"+mAppID+", "+phoneNumberStr + ", " + passwordStr);
				int connectState = -1;
				int connectNum = 0;
				do {
					connectState = NetSDK.connect();
					connectNum++;
					System.out.println("===>第 " + connectNum + " 次连接服务器...");
				} while (connectState == -1 && connectNum < 6);
				System.out.println("===>共连接服务器 " + connectNum + " 次连接服务器!");
				System.out.println("==========");
				System.out.println("===>isValid:" + NetSDK.isValid());
				System.out.println("===>isConnected:" + NetSDK.isConnected());
				if (connectState == 0) {
					// 连接网关
					NetSDK.sendConnectGwMsg(mAppID, ConstantBase.ANDROID,
							phoneNumberStr, MD5Util.encrypt(passwordStr));
				} else {
					Message msg = new Message();
					msg.what = ConstantBase.ERROR;
					msg.obj = getString(R.string.load_time_out);
					mHandler.sendMessage(msg);
				}
			};
		}.start();
	}

	@Override
	public boolean handleMessage(Message msg) {
		Logger.d(getClass().getName() + "===>handleMessage_what:"+msg.what);
		switch (msg.what) {
		case ConstantBase.LOADING:
			loginLayout.setVisibility(View.VISIBLE);
			findViewById(R.id.logo_phonenumber_layout).setVisibility(View.GONE);
			findViewById(R.id.logo_password_layout).setVisibility(View.GONE);
			findViewById(R.id.login_btn_layout).setVisibility(View.GONE);
			break;

		case ConstantBase.CONNECT_SERVER_GATEWAY:
			int result = msg.getData().getInt(ConstantBase.RESULT);
			if (result == ConstantBase.SUCCESS) {
				System.out.println("==========");
				System.out.println("===>isValid:" + NetSDK.isValid());
				System.out.println("===>isConnected:" + NetSDK.isConnected());
				setAppID(mAppID);
				String phoneNumberStr = phoneNumberEdit.getText().toString();
				setGatewayID(phoneNumberStr);
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				startActivity(intent);
				finish();
			} else {
				disLoadingView();
				if (result == 11) {
					Toast.makeText(this, "网关不在线", Toast.LENGTH_SHORT).show();
				} else if (result == 12) {
					Toast.makeText(this, "网关ID(用户名)有误", Toast.LENGTH_SHORT)
							.show();
				} else if (result == 13) {
					Toast.makeText(this, "密码有误", Toast.LENGTH_SHORT).show();
				} else if (result == 14) {
					Toast.makeText(this, "请重新连接 " + result, Toast.LENGTH_SHORT)
							.show();
				} else {
					Toast.makeText(this, "未知有误 " + result, Toast.LENGTH_SHORT)
							.show();
				}
			}
			break;
		case ConstantBase.ERROR:
			disLoadingView();
			Toast.makeText(this, (String) msg.obj, Toast.LENGTH_SHORT).show();
			break;
		}
		return false;
	}

	private void disLoadingView() {
		findViewById(R.id.logo_phonenumber_layout).setVisibility(View.VISIBLE);
		findViewById(R.id.logo_password_layout).setVisibility(View.VISIBLE);
		findViewById(R.id.login_btn_layout).setVisibility(View.VISIBLE);
		loginLayout.setVisibility(View.GONE);
	}
}
